<?php

class QinformationController extends Controller
{
	
	    public $layout = "application.views.layouts.admin";
	public $image;
	public function filters()
		{
			return array(
	              'accessControl',
			);
		}
	
		public function accessRules()
		{
			return array(
				array('allow',
	                'actions'=>array('list','add','edit','del','addcategory','Categorylist','Deletecategory','EditCategory'),
	                'roles'=>array('admin'),
			),
			
			array('deny',
	                'actions'=>array('list','add','edit','del','addcategory','Categorylist','Deletecategory','EditCategory'),
	                'users'=>array('*'),
			),
			);
		}
		
		public function actionAdd() 
		{
			$information = new Information();
      
  
       if (isset($_POST['ajax'])&& $_POST['ajax']==='information')
	            {
	                # code...
	                echo CActiveForm::validate($reguser);
	                Yii::app()->end();
	            }
        
        if (isset($_POST['Information'])) 
        {
        	 $information->attributes=$_POST['Information'];
        	 $information->image = CUploadedFile::getInstance($information,'image'); 
        	     
        		     	
				
        if ($information->validate('reg'))
			{
				
				 $information->date=date('Y-m-d H:i:s');
				 $information->id_cat=$_POST['list'];
				if($information->image!="")
				{
					 
						
				 $imgas=preg_split('/[.]+/',$information->image, -1, PREG_SPLIT_NO_EMPTY); 
				 $imgas[0]=mktime(date("s"),date("Y"),date("m"),date("d"));
					$information->image->saveAs('images/slider/'.$imgas[0].".".$imgas[1]);
					copy('images/slider/'.$imgas[0].".".$imgas[1],'images/slider/'.$imgas[0]."_thumb.".$imgas[1]);
						$information->image=$imgas[0].".".$imgas[1];
				}
				else 
				{
					$information->indexf=0;
				}
				if($information->save())
				 {	
				 	if($information->image!="")
					{			 
					 	$this->Resize($imgas[0]."_thumb.".$imgas[1],50,38);
					 	$this->Resize($imgas[0].".".$imgas[1],658,390);
					}
				 	$this->redirect("/admin/qinformation/list/");
				
				 }
			}
        	
        }
        	 
             $tagi=InformationCategory::model()->findAll();
               $list = CHtml::listData($tagi, 'id_cat', 'text');
        $this->render("application.views.qinformation.add",array('information'=>$information,'list'=>$list));
		}
		
		private function Resize($nameImage,$with,$height) 
		{
			
			
			Yii::app()->setComponents(array('imagemod'=>array('class'=>'application.extensions.imagemodifier.CImageModifier')));
				 		
				 $imga = Yii::app()->imagemod->load('images/slider/'.$nameImage);
				 $imga->image_resize          = true;
				$imga->image_y         = $height;
				$imga->image_x         = $with;
				//$imga->file_new_name_body = $img->file_src_name;
				$imga->process('images/slider/');
				if ($imga->processed) {					
					$imga->clean();
					$imga=preg_split('/[.]+/',$nameImage, -1, PREG_SPLIT_NO_EMPTY); 				
					
					rename('images/slider/'.$imga[0]."_1.".$imga[1],'images/slider/'.$nameImage);
				} else {
					echo 'error : ' . $img->error;
			}
		}
		
		private function Delete($nameImg) 
		{
			if (@fopen('images/slider/'.$nameImg, "r"))
			{
			 	$imga_del=preg_split('/[.]+/',$nameImg, -1, PREG_SPLIT_NO_EMPTY);
				unlink('images/slider/'.$nameImg);
				unlink('images/slider/'.$imga_del[0]."_thumb.".$imga_del[1]);	
			}
			
		}
		
		
		public function actionList()
		{
			$page_size =10; 
        	$information = Information::model()->recently(0,$page_size)->findAll();
        	$information_count = Information::model()->count();
          	$count_page=$information_count/$page_size;
          
          	if(!isset($_GET['page']))
          	{
          		$information=Information::model()->recently(0,$page_size)->findAll();
          	}
          	else
           	{
	           	$page=$_GET['page'];
	           	$sdvig = $page_size*($page-1);
	           	$information=Information::model()->recently( $sdvig,$page_size)->findAll();
           	}
            
            
            
           $this->render("application.views.qinformation.list", array('information' => $information,'count_page'=>$count_page+1,));
		}
		
		
		public function actionDel() {
			$inf_id = $_GET['id'];
			$inf_del =Information::model()->findByPk($inf_id);
			$this->Delete($inf_del->image);
			 
			Information::model()->deleteByPk($inf_id);
			 
			$this->redirect('/admin/qinformation/list/');
		}
    
    
    
		#Update news new style
		public function actionEdit() {
			$inf_id = $_GET['id'];

			$information=Information::model()->findByPk($inf_id);
			if (isset($_POST['ajax'])&& $_POST['ajax']==='information')
			{
				# code...
				echo CActiveForm::validate($reguser);
				Yii::app()->end();
			}

			if (isset($_POST['Information']))
			{
			 $information->attributes=$_POST['Information'];
			 $information->id_cat=$_POST['list'];
			 
				 if ($information->validate('reg'))
				 {
				 	$image=  CUploadedFile::getInstance($information,'image');
				 	if($image!=null)
				 	{
				 		$inf_delete = Information::model()->findByPk($inf_id);
				 		$this->Delete($inf_delete->image);
				 		
				 		$information->image = CUploadedFile::getInstance($information,'image');
				 		$imgas=preg_split('/[.]+/',$information->image, -1, PREG_SPLIT_NO_EMPTY);
				 
				 		$imgas[0]=mktime(date("s"),date("Y"),date("m"),date("d"));
				 		$information->image->saveAs('images/slider/'.$imgas[0].".". $imgas[1]);
				 		$information->image=$imgas[0].".". $imgas[1];
				 		copy('images/slider/'.$imgas[0].".". $imgas[1],'images/slider/'.$imgas[0]."_thumb.".$imgas[1]);
				 		$this->Resize($imgas[0]."_thumb.".$imgas[1],50,38);
				 		$this->Resize($imgas[0].".". $imgas[1],658,390);			 			
				 	}
				 				 		
					if($information->save())
					{
						$this->redirect('/admin/qinformation/list/');
					}
				 }			  
			}

			$Inf = new Information();
			$tagi=InformationCategory::model()->findAll();
			$list = CHtml::listData($tagi, 'id_cat', 'text');
			$this->render("application.views.qinformation.edit",array('information' => $information, 'Inf'=>$Inf,'list'=>$list));
		}
    
    public function actionAddcategory() 
    {
    	 $tag = new InformationCategory();
        
       if (isset($_POST['ajax'])&& $_POST['ajax']==='tag')
	            {
	                # code...
	                echo CActiveForm::validate($reguser);
	                Yii::app()->end();
	            }
        
        if (isset($_POST['InformationCategory'])) 
        {
        	
			 $tag->attributes=$_POST['InformationCategory'];			 
			//print_r($_POST['News']);
		
        if ($tag->validate('reg'))
			{
				 
			
					
				 if($tag->save())
				 {
				 	$this->redirect('/admin/qinformation/categorylist/');
				 }
			}
        	
        }
        	
        $this->render("application.views.qinformation.addcategory",array('tag'=>$tag));
    }

    
 	public function actionCategorylist() 
    {
    	$page_size =5; 
        $tag = InformationCategory::model()->recently(0,$page_size)->findAll();
        $tag_count = InformationCategory::model()->count();
          $count_page=$tag_count/$page_size;
     	if(!isset($_GET['page']))
         {         	
         		$tag=InformationCategory::model()->recently(0,$page_size)->findAll();                   
         }
         else
           {
           	$page=$_GET['page'];
             $sdvig = $page_size*($page-1);
             $tag=InformationCategory::model()->recently( $sdvig,$page_size)->findAll();
           }
    	
    	
    	$this->render("application.views.qinformation.categorylist",array('tag'=>$tag,'count_page'=>$count_page+1));
    }
    
    
    public function actionDeletecategory() 
    {
    	InformationCategory::model()->deleteByPk($_GET['id']);
    	$this->redirect("/admin/qinformation/categorylist");
    }
    
    
 public function actionEditCategory() 
    {
    	 $tag_id = $_GET['id'];
	//echo $tag_id;
	$tag=InformationCategory::model()->findByPk($tag_id);
    if (isset($_POST['ajax'])&& $_POST['ajax']==='tag')
	            {
	                # code...
	                echo CActiveForm::validate($reguser);
	                Yii::app()->end();
	            }
        
        if (isset($_POST['InformationCategory'])) 
        {
        	
			 $tag->attributes=$_POST['InformationCategory'];			 
			 
        if ($tag->validate('reg'))
			{
				 
				// $usercv->user_id = Yii::app()->user->id;
				// $tag->id_tag=$_POST['taglist'];
				 //$usercv->date=date();

				 if($tag->save())
				 {
				 	$this->redirect('/admin/qinformation/categorylist/');
				 }
			}
        	
        }
       // $news = news::model()->findByPk($news_id);
       
        $Tagi = new InformationCategory();
       
        $this->render("application.views.qinformation.editcategory",array('tag' => $tag, 'Tagi'=>$Tagi));
    }
    
	
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}